package eleven;

import java.util.Arrays;

/**
 * 2300. 咒语和药水的成功对数(中等)
 */
public class Day10 {
    public int[] successfulPairs(int[] spells, int[] potions, long success){
        Arrays.sort(potions);
        int n = spells.length, m = potions.length;
        int[] count = new int[n];
        for (int i = 0; i < n; i++) {
            long t = (success + spells[i] - 1) / spells[i];//需要找到第一个大于等于t的下标
            count[i] = m - binarySearch(potions,0,m-1,t);
        }
        return count;
    }

    public static int binarySearch(int[] potions, int l, int r, long target) {
        int res = r + 1;
        while (l <= r){
            int mid = l +((r - l) >> 1);
            if (potions[mid] >= target){
                res = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] spells = {5,1,3};
        int[] potions = {1,2,3,4,5};
        Day10 day10 = new Day10();
        int[] pairs = day10.successfulPairs(spells,potions,7);
        for (int i = 0; i < pairs.length; i++) {
            System.out.print(pairs[i]+" ");
        }
    }
}
